package com.xuhuan.demo.pattern.singleton.type6;

/**
 * @author huan.xu
 * @date 2021/10/2 23:08
 * @description 懒汉式(双重检查)
 */
public class SingletonDemo6 {

  public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
      new Thread(() -> System.out.println(Singleton.getInstence())).start();
    }
  }
}

class Singleton {

  private Singleton() {
  }

  private static volatile Singleton singleton;

  public static Singleton getInstence() {
    if (singleton == null) {
      synchronized (Singleton.class) {
        if (singleton == null) {
          singleton = new Singleton();
        }
      }
    }
    return singleton;
  }
}
